﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using PagedList;

namespace DealKhuyenMaiV2.com.Controllers
{
    [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
    public class OrderManagerController : BaseController
    {
        // GET: /OrderManager/

        public ViewResult Index()
        {
            var orders = DbConText.Orders.Include("Account").Include("Product").Include("StatusOrder").Where(p => p.StatusOrder.NameStatus != "Đã xóa");
            int pageSize = 10;
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return View(orders.ToList().ToPagedList(1, pageSize));
        }
        [HttpPost]
        public PartialViewResult Index(int? page)
        {
            var orders = DbConText.Orders.Include("Account").Include("Product").Include("StatusOrder").Where(p => p.StatusOrder.NameStatus != "Đã xóa");
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return PartialView("_ListOrder", orders.ToList().ToPagedList(pageNumber, pageSize));
        }
        [HttpPost]
        public PartialViewResult Search(int? page, string KeyWord, string Display)
        {
            var orders = DbConText.Orders.Include("Account").Include("Product").Include("StatusOrder").Where(
                    o => ((o.Product.NameProduct.IndexOf(KeyWord) > -1 || KeyWord.IndexOf((string)o.Product.NameProduct) > -1
                          )&& o.StatusOrder.NameStatus != "Đã xóa"));
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Search";
            ViewBag.Display = Display;
            ViewBag.KeyWord = KeyWord;
            return PartialView("_ListOrder", orders.ToList().ToPagedList(pageNumber, pageSize));
        }
        //
        // GET: /OrderManager/Details/5

        public ViewResult Details(int id)
        {
            Order order = DbConText.Orders.Single(o => o.IDOrder == id);
            return View(order);
        }

        public ActionResult Edit(int id)
        {
            Order order = DbConText.Orders.Single(o => o.IDOrder == id && o.StatusOrder.NameStatus != "Đã xóa");
            ViewBag.IDAccountReceive = new SelectList(DbConText.Accounts, "IDAccount", "Email", order.IDAccountReceive);
            ViewBag.IDProduct = new SelectList(DbConText.Products, "IDProduct", "NameProduct", order.IDProduct);
            ViewBag.IDStatus = new SelectList(DbConText.StatusOrders, "IDStatus", "NameStatus", order.IDStatus);
            return View(order);
        }

        //
        // POST: /OrderManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Order order)
        {
            Order _order = DbConText.Orders.Single(o => o.IDOrder == id);
            {
                _order.IDProduct = order.IDProduct;
                _order.Total = order.Total;
                _order.IDStatus = order.IDStatus;
                _order.DateAdded = order.DateAdded;
                _order.IDAccountReceive = order.IDAccountReceive;
                _order.AddressReceive = order.AddressReceive;
                _order.PhoneNumberReceive = order.PhoneNumberReceive;
                _order.NumberOfProduct = order.NumberOfProduct;
                _order.Voucher = order.Voucher;
            }

            if (ModelState.IsValid)
            {
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDAccountReceive = new SelectList(DbConText.Accounts, "IDAccount", "Email", order.IDAccountReceive);
            ViewBag.IDProduct = new SelectList(DbConText.Products, "IDProduct", "NameProduct", order.IDProduct);
            ViewBag.IDStatus = new SelectList(DbConText.StatusOrders, "IDStatus", "NameStatus", order.IDStatus);
            return View(order);
        }

     

        //
        // GET: /OrderManager/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    Order order = DbConText.Orders.Single(o => o.IDOrder == id);
        //    return View(order);
        //}

        ////
        //// POST: /OrderManager/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Order order = DbConText.Orders.Single(o => o.IDOrder == id);
        //    DbConText.Orders.DeleteObject(order);
        //    DbConText.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        [HttpPost, ActionName("Delete")]
        public PartialViewResult DeleteConfirmed(int? page, int id)
        {
            Order order = DbConText.Orders.Single(o => o.IDOrder == id);
            order.IDStatus = 5;
            DbConText.SaveChanges();

            var orders = DbConText.Orders.Include("Account").Include("Product").Include("StatusOrder").Where(p => p.StatusOrder.NameStatus != "Đã xóa");
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return PartialView("_ListOrder", orders.ToList().ToPagedList(pageNumber, pageSize));

        }

    }
}